package com.qdu.study_app.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qdu.study_app.R;
import com.qdu.study_app.bean.Clock;
import com.qdu.study_app.bean.StudyItem;
import com.qdu.study_app.fragment.StudyFragment;
import com.qdu.study_app.utils.MyToast;

import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
    private Context context;
    private List<Clock> studyItemList;
    private OnItemClickListener mOnItemClickListener;

    public MyRecyclerViewAdapter(Context context, List<Clock> studyItemList) {
        this.context = context;
        this.studyItemList = studyItemList;

    }

    /*
    * 创建ViewHolder并返回，后续item布局里控件都是从ViewHolder中取出
    * */

    @NonNull
    @Override
    public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 为每一个item设置view
        View view = LayoutInflater.from(context).inflate(R.layout.study_card,parent,false);

        return new ViewHolder(view);
    }

    /*
    * 将数据绑定到ViewHolder中,
    * 有几个数据就调用几次
    * */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 根据点击位置绑定数据
        Clock studyItem = studyItemList.get(position);
        holder.img.setImageResource(StudyFragment.imgList[studyItem.getImg_index()]);
        holder.toDoTitle.setText(String.valueOf(studyItem.getTask_name()));
        holder.toDoTime.setText(String.valueOf(studyItem.getTime()));

        // 在onBindViewHolder中调用实现类对象的onItemClick函数
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(holder.toDoTime,position);
                }

                //MyToast.showToast(context,"点击了卡片",2000);
            }
        });

    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public int getItemCount() {
        return studyItemList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView toDoTitle;
        TextView toDoTime;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.study_item_img);
            toDoTitle = (TextView)itemView.findViewById(R.id.todo_title);
            toDoTime = (TextView)itemView.findViewById(R.id.todo_time);

        }
    }

    /*
    * 设置回调接口
    * 定义item的点击事件监听器
    * */
    public interface OnItemClickListener{
        void onItemClick(View v,int position);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        // 给adapter设置点击事件实例
        this.mOnItemClickListener = onItemClickListener;
    }
}
